(function() {
    Nokia.DropDown = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				maximize: false
			};

            instance.options = jQuery.extend(defaults, options);

            instance.element = jQuery( instance.options.element );

            instance._super.apply(instance, [instance.options]);

            instance.registerData("dropdown");

            instance._create();
            instance._attachEvents();

			instance.registerVibrationOn([]);
			instance.fireCallback('create');
        },

        _attachEvents: function() {
        	var instance = this,
        		Styles = instance.Styles;

        	instance.label.bind('focus.dropdown', function() {
        		instance.topBorder.addClass(Styles.nokiaBorderStateHover);
        		instance.wrapper.addClass(Styles.uiStateHover);

        		if ( !instance.isVisible ) {
        			instance.bottomBorder.addClass(Styles.nokiaBorderStateHover);
        		}

        	});

        	instance.label.bind('blur.dropdown', function() {
        		instance.topBorder.removeClass(Styles.nokiaBorderStateHover);
        		instance.wrapper.removeClass(Styles.uiStateHover);

        		if ( !instance.isVisible ) {
        			instance.bottomBorder.removeClass(Styles.nokiaBorderStateHover);
        		}

        	});

        	var optionsAction = function() {

        		if (instance.isVisible) {
        			instance.hideOptions();
        		} else {
        			instance.showOptions();
        		}

				return false;
        	};

        	instance.image.bind('click.dropdown', optionsAction);
        	Nokia.util.bindEnter(instance.image, optionsAction);

        	instance.label.bind('click.dropdown', optionsAction);
        	Nokia.util.bindEnter(instance.label, optionsAction);
        },

        _cleanHoverState: function() {
        	var instance = this;
        	var Styles = instance.Styles;

        	jQuery.each(instance.items, function(i, item) {
        		Nokia.util.removeClass( item[0], Styles.uiStateHover );
        	});

        },

		_create: function() {
			var instance = this;
			var element = instance.element;
			var options = instance.options;
			var Styles = instance.Styles;
			var items = options.items;

			if (options.maximize) {
				Nokia.util.maximize(element);
			}

			Nokia.util.addClass( element[0], [Styles.uiClearfix, Styles.dropdown].join(' ') );

			var wrapper = Nokia.dom.parseHTML('<div class="' + [Styles.dropownItemsWrapper, Styles.uiClearfix, Styles.uiStateDefault].join(' ') + '"></div>');
			var label = Nokia.dom.parseHTML('<a href="javascript:void(0);" class="' + [Styles.dropdownLabel].join(' ') + '"></a>');
			var image = Nokia.dom.parseHTML('<a href="javascript:void(0);" class="' + [Styles.dropdownArrow].join(' ') + '"></a>');

			instance.wrapper = wrapper;
			instance.label = label;
			instance.image = image;

			Nokia.dom.append(element, wrapper);
			Nokia.dom.append(wrapper, label);
			Nokia.dom.append(wrapper, image);

			instance.items = [];

			var itensWrapper = Nokia.dom.parseHTML('<div class="' + Styles.uiStateDefault + '"></div>');
			var itensWrapperContainer = Nokia.dom.parseHTML('<div class="' + Styles.dropdownItensContainer +'"></div>');

			Nokia.dom.append(element, itensWrapperContainer);
			Nokia.dom.append(itensWrapperContainer, itensWrapper);

			jQuery.each(items, function(i, item) {
				var itemContainer = Nokia.dom.parseHTML('<a href="javascript:void(0);" class="' + [Styles.dropdownItem].join(' ') + '">' + item.label +'</a>');

				var itemAction = function(event) {
					instance.select(i, event);
				};

				itemContainer.bind('click.dropdown', itemAction);
				Nokia.util.bindEnter(itemContainer, itemAction);

				itemContainer.bind('focus.dropdown', function() {
					Nokia.util.addClass( itemContainer[0], Styles.uiStateHover );
				});

				itemContainer.bind('blur.dropdown', function() {
					Nokia.util.removeClass( itemContainer[0], Styles.uiStateHover );
				});

				itemContainer.hover(
	        		function() {
	        			itemContainer.trigger('focus.dropdown');
	        		},
	        		function() {
	        			itemContainer.trigger('blur.dropdown');
	        		}
	    		);

				instance.items.push(itemContainer);

				Nokia.dom.append(itensWrapper, itemContainer);
			});

			Nokia.util.applyRoundedCorners(wrapper,'', 'top');
			Nokia.util.applyRoundedCorners(itensWrapper,'', 'bottom');

			instance.topBorder = instance.element.find('.nokia-border-top');
        	instance.bottomBorder = instance.element.find('.nokia-border-bottom');

			instance.itensWrapper = itensWrapper;

			instance.select(0, null);
		},

		showOptions: function() {
			var instance = this;
			var Styles = instance.Styles;

			instance.isVisible = true;

			instance.element.find('.' + Styles.dropdownItem + '').css('display', 'block');

			instance.bottomBorder.removeClass(Styles.nokiaBorderStateHover);

			instance.fireCallback('show');
		},

		hideOptions: function() {
			var instance = this;
			var Styles = instance.Styles;

			instance.isVisible = false;

			instance.element.find('.' + Styles.dropdownItem + '').hide();

			instance.label.focus();

			instance.fireCallback('hide');
		},

		select: function(i, event) {
			var instance = this;
			var options = instance.options;
			var element = instance.element;
			var Styles = instance.Styles;
			var item = options.items[i];
			var itemElement = instance.items[i];

			if (instance.selectedElement) {
				Nokia.util.removeClass( instance.selectedElement[0], Styles.dropdownSelected );
			}

			Nokia.util.addClass( itemElement[0], Styles.dropdownSelected );

			instance.label.html(item.label);

			instance.selectedItem = item;
			instance.selectedElement = itemElement;

			// Non-touch fix: clean hover classes
			instance._cleanHoverState();

			if (event && item.select) {
				item.select.apply(instance, [item, event]);
			}

			instance.hideOptions();

		},

		getSelected: function() {
			var instance = this;

			return instance.selectedItem;
		},

		Styles: {
			dropdown: 'nokia-dropdown',
			dropdownItem: 'nokia-dropdown-item',
			dropdownItensContainer: 'nokia-dropdown-container',
			dropdownArrow: 'nokia-dropdown-arrow',
			dropdownArrowUp: 'nokia-dropdown-arrow-up',
			dropdownLabel: 'nokia-dropdown-label',
			dropdownSelected: 'nokia-dropdown-item-selected',
			dropdownItemsWrapper: 'nokia-dropdown-item-wrapper',
			uiClearfix: 'ui-helper-clearfix',
			uiStateDefault: 'ui-state-default',
			uiStateHighlight: 'ui-state-active',
			uiStateHover: 'ui-state-hover',
			uiStateActive: 'ui-state-active',
			nokiaBorderStateHover: 'nokia-border-state-hover'
		}
    });
})();